package com.lindroid.androiduilearning.textview;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

import com.lindroid.androiduilearning.R;
import com.lindroid.androiduilearning.base.BaseActivity;
import com.lindroid.androiduilearning.base.BaseFragment;
import com.lindroid.androiduilearning.textview.fragment.AutoSizeFragment;
import com.lindroid.androiduilearning.textview.fragment.CustomFontFragment;
import com.lindroid.androiduilearning.textview.fragment.TextStyleFragment;
import com.lindroid.androiduilearning.textview.fragment.ViewTreeFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linyulong
 */
public class TextViewActivity extends BaseActivity {

    /**
     * 字体自动缩放的属性
     */
    private final int AUTO_SIZE = 1;
    private final int TEXT_STYLE = 2;
    private final int CUSTOM_VIEW = 3;
    private final int VIEW_TREE = 4;
    /**
     * Fragment的集合
     */
    private List<BaseFragment> fragmentList;
    /**
     * 被选中的fragment的position
     */
    private int position;

    /**
     * 上次切换的Fragment
     */
    private BaseFragment currentFragment;

    private String[] menuTitles = {"autoSize", "字体属性", "自定义View改变字体", "遍历ViewTree替换字体"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        initBlueToolBar(getString(R.string.text_view), true);
        //初始化Fragment
        initFragment();
        setFirstFragment();

    }

    private void setFirstFragment() {
        //默认显示第一个Fragment
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.view_content, fragmentList.get(0)).commit();
        //将第一个Fragment记录为当前的Fragment
        currentFragment = fragmentList.get(0);
    }

    private void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new AutoSizeFragment());
        fragmentList.add(new TextStyleFragment());
        fragmentList.add(new CustomFontFragment());
        fragmentList.add(new ViewTreeFragment());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, AUTO_SIZE, 1, menuTitles[0]);
        menu.add(Menu.NONE, TEXT_STYLE, 2, menuTitles[1]);
        menu.add(Menu.NONE, CUSTOM_VIEW, 3, menuTitles[2]);
        menu.add(Menu.NONE, VIEW_TREE, 4, menuTitles[3]);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case AUTO_SIZE:
                position = item.getOrder() - 1;
                break;
            case TEXT_STYLE:
                position = item.getOrder() - 1;
                break;
            case CUSTOM_VIEW:
                position = item.getOrder() - 1;
                break;
            case VIEW_TREE:
                position = item.getOrder() - 1;
                break;
            default:
                break;
        }
        //获取position对应的Fragment
        BaseFragment nextFragment = getFragment(fragmentList, position);
        //切换Fragment
        currentFragment = switchFragment(R.id.view_content, currentFragment, nextFragment);
        return super.onOptionsItemSelected(item);
    }


}
